//
//  NEDAlertView.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/10/28.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "NEDAlertView.h"
#import "UIButton+NED.h"
#import "NEDDefines.h"
#import "NEDConstant.h"
#import "UIColor+NEDStyle.h"
#import "UIFont+NED.h"
#import "NEDDeviceInfo.h"


@interface NEDAlertView()

@property (retain, nonatomic) UIButton * contentViewTitle;//顶部标题
@property (retain, nonatomic) UILabel * contentViewText;//内容
@property (retain, nonatomic) UIView * contentViewCustom;//自定义视图
@property (retain, nonatomic) UIScrollView * contentViewScrollV;//内容
@property (retain, nonatomic) UIStackView * contentViewButtons;//底部按钮视图
@property (retain, nonatomic, readwrite) NEDAlertLayoutConfig * layoutConfig;// 布局信息

@property (assign, nonatomic) CGFloat customViewHeight;

/// 底部按钮下方的提示
@property (retain, nonatomic) UILabel * hintLabelBottom;
/// 底部按钮上方的提示
@property (retain, nonatomic) UILabel * hintLabelTop;

@end

@implementation NEDAlertView

- (void)ned_setupUI{
    
    self.layoutConfig = [self defaultLayoutConfig];
    self.tapButtonToClose = YES;
    self.tapBackgroundToClose = NO;
    self.isBlurShow = NO;

    self.backgroundView.backgroundColor = [[UIColor ned_black] colorWithAlphaComponent:0.8];

    self.contentView.backgroundColor = [UIColor ned_background];
    
    {
        self.contentViewTitle = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.contentViewTitle.titleLabel.numberOfLines = 0;
        self.contentViewTitle.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.contentViewTitle.userInteractionEnabled = NO;
        [self.contentView addSubview:self.contentViewTitle];
    }
    
    {
        self.contentViewScrollV = [UIScrollView new];
        self.contentViewScrollV.showsVerticalScrollIndicator = NO;
        [self.contentView addSubview:self.contentViewScrollV];
        
        self.contentViewText = [UILabel new];
        self.contentViewText.textAlignment = self.layoutConfig.contentTextAligment;
        self.contentViewText.textColor = self.layoutConfig.colorContentText;
        self.contentViewText.font = self.layoutConfig.fontContentText;
        self.contentViewText.backgroundColor = [UIColor clearColor];
        self.contentViewText.numberOfLines = 0;
        self.contentViewText.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentViewScrollV addSubview:self.contentViewText];
        
        self.contentViewCustom = [UIView new];
        [self.contentViewScrollV addSubview:self.contentViewCustom];
    }
    
    {
        self.contentViewButtons = [UIStackView new];
        [self.contentView addSubview:self.contentViewButtons];
    }
    
//    self.contentViewTitle.backgroundColor = [UIColor ned_randomColor];
//    self.contentViewText.backgroundColor = [UIColor ned_randomColor];
//    self.contentViewScrollV.backgroundColor = [UIColor ned_randomColor];
//    self.contentViewButtons.backgroundColor = [UIColor ned_randomColor];
//    self.contentViewCustom.backgroundColor = [UIColor ned_randomColor];
    
//    self.contentViewTitle.backgroundColor = [UIColor clearColor];
//    self.contentViewText.backgroundColor = [UIColor clearColor];
//    self.contentViewScrollV.backgroundColor = [UIColor clearColor];
//    self.contentViewButtons.backgroundColor = [UIColor clearColor];
//    self.contentViewCustom.backgroundColor = [UIColor clearColor];

    [self alert_buildContentView];
    [self alert_config:self.layoutConfig];
}

- (void)alert_buildContentView{
    
}
- (void)alert_config:(NEDAlertLayoutConfig *)config{
    
}
- (void)alert_layoutUpdate{
    [self layoutContentViews];
}

#pragma mark - config

/// 配置信息
- (void)layoutStyleConfig:(void(^)(NEDAlertLayoutConfig * config))config{
    if(config){
        config(self.layoutConfig);
    }
}
- (void)layoutConfigWithStyle:(NEDAlertLayoutConfig *)style{
    if(IsNull(style)){
        return;
    }
    self.layoutConfig = style;
}


+ (void)showConfig:(void (^)(NEDAlertView * _Nonnull))config{
    NEDAlertView * alertView = [NEDAlertView new];
    if(config){
        config(alertView);
    }
    alertView.tapBackgroundToClose = NO;
#if TARGET_IPHONE_SIMULATOR
    alertView.tapBackgroundToClose = YES;
#endif
    [alertView popShow];
}

#pragma mark - head title

/// 添加顶部标题
- (void)addHeadTitle:(NSString *)title{
    [self addHeadTitle:title attTitle:nil];
}
- (void)addHeadTitle:(NSString *)title attTitle:(void(^ _Nullable)(NSMutableAttributedString * attTitle))attBlock{
    NSMutableAttributedString * attStr = [NSMutableAttributedString ned_initWithStr:title customBlock:^(NSMutableAttributedString *att) {
        [att ned_font:self.layoutConfig.fontHeadTitle];
        [att ned_color:self.layoutConfig.colorHeadTitle];
        [att ned_paragraphBlock:^(NSMutableParagraphStyle *style) {
            style.alignment = self.layoutConfig.titleAligment;
            style.lineBreakMode = NSLineBreakByCharWrapping;
        }];
        if(attBlock){
            attBlock(att);
        }
    }];
    [self.contentViewTitle setAttributedTitle:attStr forState:(UIControlStateNormal)];
}
- (void)addHeadAttribute:(void(^ _Nullable)(NSMutableAttributedString * attContent))attBlock{
    NSMutableAttributedString * attStr = [NSMutableAttributedString new];
    [attStr ned_color:self.layoutConfig.colorHeadTitle];
    [attStr ned_font:self.layoutConfig.fontHeadTitle];
    [attStr ned_paragraphBlock:^(NSMutableParagraphStyle *style) {
        style.alignment = self.layoutConfig.titleAligment;
        style.lineBreakMode = NSLineBreakByCharWrapping;
    }];
    if(attBlock){
        attBlock(attStr);
    }
    [self.contentViewTitle setAttributedTitle:attStr forState:(UIControlStateNormal)];
}
/// 添加顶部背景图
- (void)addHeadBackgroundImage:(UIImage *)image{
    [self.contentViewTitle setBackgroundImage:image forState:(UIControlStateNormal)];
}
- (void)addHeadBackgroundColor:(UIColor *)color{
    self.contentViewTitle.backgroundColor = color;
}
- (void)addHeadImage:(UIImage *)image{
    [self.contentViewTitle setImage:image forState:UIControlStateNormal];
}
#pragma mark - content


/// 添加内容
- (void)addContent:(NSString *)content{
    [self addContent:content attContent:nil];
}
- (void)addContent:(NSString *)content attContent:(void(^ _Nullable)(NSMutableAttributedString * attContent))attBlock{
     NSMutableAttributedString * attStr = [NSMutableAttributedString ned_initWithStr:content customBlock:^(NSMutableAttributedString *att) {
         [att ned_color:self.layoutConfig.colorContentText];
         [att ned_font:self.layoutConfig.fontContentText];
         [att ned_paragraphBlock:^(NSMutableParagraphStyle *style) {
             style.alignment = self.layoutConfig.contentTextAligment;
             style.lineBreakMode = NSLineBreakByCharWrapping;
         }];
         if(attBlock){
             attBlock(att);
         }
     }];
    self.contentViewText.attributedText = attStr;
}
- (void)addContentAttribute:(void (^)(NSMutableAttributedString * _Nonnull))attBlock{
    NSMutableAttributedString * attStr = [NSMutableAttributedString new];
    [attStr ned_color:self.layoutConfig.colorContentText];
    [attStr ned_font:self.layoutConfig.fontContentText];
    [attStr ned_paragraphBlock:^(NSMutableParagraphStyle *style) {
        style.alignment = self.layoutConfig.contentTextAligment;
        style.lineBreakMode = NSLineBreakByCharWrapping;
    }];
    if(attBlock){
        attBlock(attStr);
    }
    self.contentViewText.attributedText = attStr;
}

#pragma mark - add custom view

/// 添加自定义视图显示弹窗
- (void)addCustomView:(UIView *)customView{
    [self addCustomView:customView contentHeight:0];
}
- (void)addCustomView:(UIView *)customView contentHeight:(CGFloat)contentHeight{
    [self.contentViewCustom.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.contentViewCustom addSubview:customView];
    if(contentHeight <= 0){
        contentHeight = CGRectGetHeight(customView.frame);
    }
    self.customViewHeight = contentHeight;
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentViewCustom);
        make.height.mas_equalTo(contentHeight);
    }];
}

#pragma mark - buttons

/// 从左到右 从上到下
- (UIButton *)addBtnTitle:(NSString *)btnTitle action:(NEDAlertButtonClickAction)action{
    return [self addBtnBlock:nil action:action]
        .nedTitle(btnTitle)
        .nedFont(17);
}
- (UIButton *)addBtnBlock:(void(^_Nullable)(UIButton * passBtn))btnBlock action:(NEDAlertButtonClickAction)action{
    if(self.contentViewButtons.arrangedSubviews.count >= 5){
        NSLog(@">>>>>>> Error: 最多五个选项按钮");
        return nil;
    }
    UIButton * alertBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    alertBtn
        .nedTitleColor(self.layoutConfig.colorBottomButtonForTitle).nedBackgroundColor(self.layoutConfig.colorBottomButtonBackground);
    alertBtn.titleLabel.font = self.layoutConfig.fontBottomButtonForTitle;
    if(self.layoutConfig.showBottomButtonCorner){
        if(self.layoutConfig.bottomButtonCornerRadius){
            alertBtn.layer.cornerRadius = self.layoutConfig.bottomButtonCornerRadius;
        }else{
            alertBtn.layer.cornerRadius = self.layoutConfig.buttonHeight*0.5;
        }
    }
    if(self.layoutConfig.bottomButtonBorderWidth > 0){
        alertBtn.layer.borderWidth = self.layoutConfig.bottomButtonBorderWidth;
        alertBtn.layer.borderColor = self.layoutConfig.bottomButtonBorderColor.CGColor;
    }
    
    WS(weakSelf);
    [alertBtn addActionHandler:^(UIButton * sender, NSInteger tag) {
        if(action){
            action(weakSelf);
        }
        if(weakSelf.tapButtonToClose){
            [weakSelf popDismiss];
        }
    }];
    if(btnBlock){
        btnBlock(alertBtn);
    }
    [self.contentViewButtons addArrangedSubview:alertBtn];
    return alertBtn;
}
- (UIButton *)addBtnCancel{
    return [self addBtnCloseTitle:@"取消"];
}
- (UIButton *)addBtnCloseTitle:(NSString *)closeTitle{
    return [self addBtnTitle:closeTitle action:nil];
}

/// 在按钮上方添加提示
- (void)addHintButtonsTop:(NSString * _Nullable)hint config:(void(^_Nullable)(UILabel * hintLabel))config{
    if(self.hintLabelTop){
        [self.hintLabelTop removeFromSuperview];
    }
    
    self.hintLabelTop = [UILabel new];
    self.hintLabelTop.textColor = UIColor.ned_hint;
    self.hintLabelTop.textAlignment = NSTextAlignmentCenter;
    self.hintLabelTop.numberOfLines = 0;
    self.hintLabelTop.font = [UIFont ned_pfOfSize:12];
    [self.contentView addSubview:self.hintLabelTop];
    [self.hintLabelTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentViewButtons);
        make.bottom.equalTo(self.contentViewButtons.mas_top).offset(-7);
    }];
    if(IsNonNull(hint)){
        self.hintLabelTop.text = hint;
    }
    if(config){
        config(self.hintLabelTop);
    }
}
/// 在按钮下方添加提示
- (void)addHintButtonsBottom:(NSString * _Nullable)hint config:(void(^_Nullable)(UILabel * hintLabel))config{
    if(self.hintLabelBottom){
        [self.hintLabelBottom removeFromSuperview];
    }
    
    self.hintLabelBottom = [UILabel new];
    self.hintLabelBottom.textColor = UIColor.ned_hint;
    self.hintLabelBottom.textAlignment = NSTextAlignmentCenter;
    self.hintLabelBottom.numberOfLines = 0;
    self.hintLabelBottom.font = [UIFont ned_pfOfSize:12];
    [self.contentView addSubview:self.hintLabelBottom];
    [self.hintLabelBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentViewButtons);
        make.top.equalTo(self.contentViewButtons.mas_bottom).offset(7);
    }];
    if(IsNonNull(hint)){
        self.hintLabelBottom.text = hint;
    }
    if(config){
        config(self.hintLabelBottom);
    }
}


/// 内容
- (UILabel *)alertContentViewText{
    return self.contentViewText;
}

#pragma mark - layout views

- (void)layoutContentViews{
    
    self.contentView.layer.cornerRadius = self.layoutConfig.contentViewCornerRadius;
    self.contentView.layer.borderWidth = self.layoutConfig.contentViewBorderWidth;
    self.contentView.layer.borderColor = self.layoutConfig.contentViewBorderColor.CGColor;
    self.contentView.layer.masksToBounds = YES;

    
    __block CGFloat contentHeight = 0;
    CGFloat contentWidth = self.layoutConfig.contentWidth;
    
    /// MARK: - title height
    __block BOOL titleIsEmpty = NO;
    {
        void(^remakrConstraints)(CGFloat height) = ^(CGFloat height) {
            if(height > 0){
                [self.contentViewTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(height);
                    make.left.offset(self.layoutConfig.spaceTitleMargin);
                    make.right.offset(-self.layoutConfig.spaceTitleMargin);
                    make.top.offset(self.layoutConfig.spaceTitleToTop);
                }];
                contentHeight = contentHeight + height + self.layoutConfig.spaceTitleToTop;
            }else{
                titleIsEmpty = YES;
                [self.contentViewTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(0);
                    make.right.left.top.offset(0);
                }];
            }
        };
        
        CGFloat titleWidth = contentWidth - self.layoutConfig.spaceTitleMargin*2;
        
        NSAttributedString * att = [self.contentViewTitle currentAttributedTitle];
        if(self.layoutConfig.contentTitleFixHeight){
            remakrConstraints(self.layoutConfig.contentTitleFixHeight);
        }
        else if((IsNonNull(att) && IsNonNull(att.string))){
            CGSize contentSize = [att ned_getSizeConstrainedToSize:CGSizeMake(titleWidth, FLT_MAX)];
            if(CGSizeEqualToSize(contentSize, CGSizeZero)){
                contentSize = [self.contentViewTitle sizeThatFits:CGSizeMake(titleWidth, FLT_MAX)];
            }
            if(CGSizeEqualToSize(contentSize, CGSizeZero)){
                remakrConstraints(0);
            }else{
                remakrConstraints(contentSize.height);
            }
        }
        else if ([self.contentViewTitle currentBackgroundImage]){
            UIImage * backgroundImage = [self.contentViewTitle currentBackgroundImage];
            CGSize imageSize = backgroundImage.size;
            if(CGSizeEqualToSize(imageSize, CGSizeZero)){
                remakrConstraints(0);
            }else{
                CGFloat titleHeight = (titleWidth*imageSize.height)/imageSize.width;
                remakrConstraints(titleHeight);
            }
        }else if([self.contentViewTitle currentImage] || [self.contentViewTitle currentTitle]){
            self.contentViewTitle.frame = CGRectMake(0, 0, titleWidth, 0);
            [self.contentViewTitle sizeToFit];
            CGFloat titleHeight = ceil(self.contentViewTitle.frame.size.height);
            remakrConstraints(titleHeight);
        }else{
            remakrConstraints(0);
        }
    }
    
    /// MARK: - scrollview height
    __block BOOL contentIsEmpty = NO;
    {
        CGFloat scrollWidth = contentWidth - self.layoutConfig.spaceContentMargin*2;
        __block CGFloat contentHeightText = 0;
        __block CGFloat contentHeightCustom = 0;
        __block CGFloat contentHeightScroll = 0;
        
        /// MARK: text height
        {
            NSAttributedString * att = self.contentViewText.attributedText;
            if((IsNonNull(att) && IsNonNull(att.string))){
                CGSize contentSize = [att ned_getSizeConstrainedToSize:CGSizeMake(scrollWidth, FLT_MAX)];
                if(CGSizeEqualToSize(contentSize, CGSizeZero)){
                    contentSize = [self.contentViewText sizeThatFits:CGSizeMake(scrollWidth, FLT_MAX)];
                }
                if(!CGSizeEqualToSize(contentSize, CGSizeZero)){
                    contentHeightText = contentSize.height;
                }
            }
        }
        
        /// MARK: custom height
        {
            if(self.contentViewCustom.subviews.count > 0){
                if(self.customViewHeight){
                    contentHeightCustom = self.customViewHeight;
                }else{
                    NSLog(@">>>>>> Error ：外部没有传customview的具体高度值");
                }
            }
        }
        
        // scrollview
        {
            void(^remakrConstraintsScrollView)(void) = ^(void) {
                if(contentHeightScroll > 0){
                    contentHeightScroll = MAX(contentHeightScroll, self.layoutConfig.contentScrollHeightMin);
                    contentHeightScroll = MIN(contentHeightScroll, self.layoutConfig.contentScrollHeightMax);
                    
                    [self.contentViewScrollV mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.offset(self.layoutConfig.spaceContentMargin);
                        make.width.mas_equalTo(scrollWidth);
                        make.top.equalTo(self.contentViewTitle.mas_bottom).offset(self.layoutConfig.spaceTitleToContent);
                    }];
                    
                    contentHeight = contentHeight + contentHeightScroll + self.layoutConfig.spaceTitleToContent;
                }else{
                    [self.contentViewScrollV mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.height.width.mas_equalTo(0);
                        make.top.equalTo(self.contentViewTitle.mas_bottom);
                        make.left.offset(0);
                    }];
                    contentIsEmpty = YES;
                }
            };
            
            void(^remakrConstraintsText)(void) = ^(void) {
                if(contentHeightText > 0){
                    if(contentHeightCustom > 0){
                        [self.contentViewText mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.height.mas_equalTo(contentHeightText);
                            make.centerX.offset(0);
                            make.width.mas_lessThanOrEqualTo(scrollWidth);
                            make.top.offset(0);
                        }];
                    }else{
                        /// 无customview信息 让text内容居中
                        [self.contentViewText mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.centerX.offset(0);
                            make.width.mas_lessThanOrEqualTo(scrollWidth);
                            make.top.offset(0);
                            make.height.mas_equalTo(MAX(contentHeightScroll, contentHeightText));
                        }];
                    }
                }else{
                    [self.contentViewText mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.height.mas_equalTo(0);
                        make.top.offset(0);
                        make.left.right.offset(0);
                    }];
                }
            };

            void(^remakrConstraintsCustom)(void) = ^(void) {
                if(contentHeightCustom > 0){
                    if(contentHeightText > 0){
                        [self.contentViewCustom mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.height.mas_equalTo(contentHeightCustom);
                            make.left.offset(0);
                            make.width.mas_equalTo(scrollWidth);
                            make.top.equalTo(self.contentViewText.mas_bottom).offset(self.layoutConfig.spaceContentTextToCustomView);
                            make.bottom.offset(0);
                        }];
                    }else{
                        /// 无text信息 让custom内容居中
                        [self.contentViewCustom mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.left.offset(0);
                            make.width.mas_equalTo(scrollWidth);
                            make.top.equalTo(self.contentViewText.mas_bottom).offset(self.layoutConfig.spaceContentTextToCustomView);
                            make.height.mas_equalTo(contentHeightCustom);
                            make.bottom.offset(0);
                        }];
                    }
                }else{
                    [self.contentViewCustom mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.height.mas_equalTo(0);
                        make.top.equalTo(self.contentViewText.mas_bottom);
                        make.left.offset(0);
                        make.bottom.offset(0);
                    }];
                }
            };
            
            contentHeightScroll = contentHeightText + contentHeightCustom;
            if(contentHeightCustom > 0 &&
               self.layoutConfig.spaceContentTextToCustomView > 0){
                contentHeightScroll += self.layoutConfig.spaceContentTextToCustomView;
            }
            
            if(self.layoutConfig.contentScrollFixHeight){
                contentHeightScroll = self.layoutConfig.contentScrollFixHeight;
                remakrConstraintsScrollView();
            }
            else{
                remakrConstraintsScrollView();
            }
            
            if(contentHeightScroll < self.layoutConfig.contentScrollHeightMax){
                /// 如果内容不多 则不裁剪子视图  为了角标显示
                self.contentViewScrollV.clipsToBounds = NO;
            }else{
                self.contentViewScrollV.clipsToBounds = YES;
            }
            
            remakrConstraintsText();
            remakrConstraintsCustom();
        }
    }
    
    /// MARK: - button height
    __block BOOL bottomButtonIsEmpty = NO;
    {
        __block CGFloat contentBottomHeight = 0;
        
        void(^remakrConstraintsBottomView)(void) = ^(void) {
            if(contentBottomHeight > 0){
                [self.contentViewButtons mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(contentBottomHeight);
                    make.left.offset(self.layoutConfig.spaceBottomButtonMargin);
                    make.right.offset(-self.layoutConfig.spaceBottomButtonMargin);
                    make.top.equalTo(self.contentViewScrollV.mas_bottom).offset(self.layoutConfig.spaceContentViewToButtons);
                    make.bottom.offset(-self.layoutConfig.spaceButtonsToBottom);
                }];
                contentHeight = contentHeight + contentBottomHeight + self.layoutConfig.spaceButtonsToBottom + self.layoutConfig.spaceContentViewToButtons;
            }else{
                [self.contentViewButtons mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.height.width.mas_equalTo(0);
                    make.left.offset(0);
                    make.top.equalTo(self.contentViewScrollV.mas_bottom);
                    make.bottom.offset(0);
                }];
            }
        };
        
        UIView *(^createLineV)(void) = ^(void){
            UIView * lineV = [UIView new];
            lineV.backgroundColor = self.layoutConfig.colorButtonsSeparationLine;
            return lineV;
        };

        NSArray * subViews = self.contentViewButtons.arrangedSubviews;
        if(subViews.count > 0 && self.layoutConfig.showButtonsSeparationLine){
            UIView * lineV = createLineV();
            [self.contentView addSubview:lineV];
            [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.equalTo(self.contentViewButtons);
                make.height.mas_equalTo(1);
            }];
        }
        
        //超过两个按钮 或者 需要垂直布局的时候 垂直布局
        if(subViews.count > 2 ||
           self.layoutConfig.buttonLayoutIsVertical){
            
            contentBottomHeight = MAX((subViews.count-1), 0)*self.layoutConfig.spaceBottomButtonVertical + subViews.count*self.layoutConfig.buttonHeight;
            
            self.contentViewButtons.spacing = self.layoutConfig.spaceBottomButtonVertical;
            self.contentViewButtons.distribution = UIStackViewDistributionEqualSpacing;
            self.contentViewButtons.axis = UILayoutConstraintAxisVertical;
            self.contentViewButtons.alignment = UIStackViewAlignmentFill;
            
            [subViews mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.layoutConfig.buttonHeight);
            }];
            
            if(self.layoutConfig.showButtonsSeparationLine){
                for(NSInteger i = 0; i<subViews.count-1; i++){
                    UIView * btnV = subViews[i];
                    UIView * lineV = createLineV();
                    [self.contentView addSubview:lineV];
                    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.bottom.equalTo(btnV).offset(self.layoutConfig.spaceBottomButtonVertical*0.5);
                        make.width.equalTo(self.contentViewButtons).multipliedBy(0.9);;
                        make.centerX.offset(0);
                        make.height.mas_equalTo(1);
                    }];
                }
            }
        }else if(subViews.count > 0){
            contentBottomHeight = self.layoutConfig.buttonHeight;
            
            self.contentViewButtons.spacing = self.layoutConfig.spaceBottomButtonHorizontal;
            self.contentViewButtons.distribution = UIStackViewDistributionFillEqually;
            self.contentViewButtons.axis = UILayoutConstraintAxisHorizontal;
            self.contentViewButtons.alignment = UIStackViewAlignmentCenter;
            
            [subViews mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.layoutConfig.buttonHeight);
            }];
            
            if(self.layoutConfig.showButtonsSeparationLine){
                for(NSInteger i = 0; i<subViews.count-1; i++){
                    UIView * btnV = subViews[i];
                    UIView * lineV = createLineV();
                    [self.contentView addSubview:lineV];
                    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.right.equalTo(btnV).offset(self.layoutConfig.spaceBottomButtonHorizontal*0.5);
                        make.height.equalTo(self.contentViewButtons).multipliedBy(0.9);
                        make.centerY.equalTo(btnV);
                        make.width.mas_equalTo(1);
                    }];
                }
            }
        }else{
            bottomButtonIsEmpty = YES;
        }

        remakrConstraintsBottomView();
    }
    
    if(titleIsEmpty &&
       contentIsEmpty &&
       bottomButtonIsEmpty){
        NSLog(@">>>>>> 弹窗内容是空的");
        [self popDismiss];
        return;
    }
    
    contentHeight = MAX(contentHeight, self.layoutConfig.contentHeightMin);
    contentHeight = MIN(contentHeight, self.layoutConfig.contentHeightMax);
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(contentWidth);
        make.height.mas_equalTo(contentHeight);
    }];
    
    if(IsNonNull(self.layoutConfig.contentBackgroundColor)){
        self.contentView.backgroundColor = self.layoutConfig.contentBackgroundColor;
    }
    if(self.layoutConfig.contentBackgroundImage){
        self.contentView.layer.contents = (id)self.layoutConfig.contentBackgroundImage.CGImage;
        self.contentView.backgroundColor = [UIColor clearColor];
    }
}

#pragma mark - pop protocol

-(void)ned_popShowAnimation{
    [self layoutContentViews];
    [super ned_popShowAnimation];
}
//
//+ (void)alertWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle actionTitles:(NSArray <NSString *> *)actionTitles attributedBlock:(void (^)(NSMutableAttributedString *att))attributedBlock handler:(void(^)(NSUInteger buttonIndex, NSString *buttonTitle))handler {
//
//    NEDAlertView * alertV = [NEDAlertView new];
//    if (IsNonNull(title)) {
//        [alertV addHeadTitle:title];
//    }
//    if (IsNonNull(message)) {
//        [alertV addContent:message attContent:attributedBlock];
//    }
//    if (IsNonNull(cancelTitle)) {
//        [alertV addBtnBlock:^(UIButton *passBtn) {
//            [passBtn setTitle:cancelTitle forState:UIControlStateNormal];
//            passBtn.titleLabel.font = [UIFont ned_pfOfSize:17];
//
//        } action:^(NEDAlertView * _Nonnull alertV) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [alertV popDismiss];
//                if (handler) {
//                    handler(0, cancelTitle);
//                }
//            });
//        }];
//    }
//    for (int i = 0; i < actionTitles.count; i++) {
//        NSString *actionTitle = actionTitles[i];
//        [alertV addBtnBlueTitle:actionTitle action:^(NEDAlertView * _Nonnull alertV) {
//            //TODO:去充值
//            if (handler) {
//                handler(i + IsNonNull(cancelTitle), actionTitle);
//            }
//        }];
//    }
//    [alertV popShow];
//}

@end

@implementation NEDAlertView(Add)

/// 默认的弹窗样式
- (NEDAlertLayoutConfig * _Nullable)defaultLayoutConfig{
    return [NEDAlertLayoutConfig new];
}


@end

@implementation NEDAlertLayoutConfig

-(void)ned_commonInit{
    self.contentBackgroundColor = [UIColor ned_background];
    self.colorHeadTitle = [UIColor ned_h1];
    self.fontHeadTitle = [UIFont ned_pfmOfSize:15];
    self.colorContentText = [UIColor ned_h2];
    self.fontContentText = [UIFont ned_pfOfSize:14];
    self.colorBottomButtonForTitle = [UIColor ned_h1];
    self.colorBottomButtonBackground = UIColor.clearColor;
    self.fontBottomButtonForTitle = [UIFont ned_pfOfSize:15];
    
    self.spaceTitleToTop = 10;
    self.spaceTitleToContent = 8;
    self.spaceContentTextToCustomView = 8;
    self.spaceContentViewToButtons = 8;
    self.spaceButtonsToBottom = 10;
    self.spaceTitleMargin = 0;
    self.spaceContentMargin = 0;
    self.spaceBottomButtonVertical = 10;
    self.spaceBottomButtonHorizontal = 10;
    self.spaceBottomButtonMargin = self.spaceBottomButtonHorizontal;

    self.contentHeightMin = 60;
    self.contentHeightMax = [NEDDeviceInfo s_height]*0.8;
    self.contentWidth = [NEDDeviceInfo s_width]*0.8;
    self.buttonHeight = 50;
    self.contentScrollHeightMax = [NEDDeviceInfo s_height]*0.5;
    self.contentScrollHeightMin = 40;

    self.showButtonsSeparationLine = NO;
    self.buttonLayoutIsVertical = NO;
    self.titleAligment = NSTextAlignmentCenter;
    self.contentTextAligment = NSTextAlignmentCenter;
    self.colorButtonsSeparationLine = [UIColor ned_separator];
    self.bottomButtonBorderColor = [UIColor ned_border];
    
    self.contentViewCornerRadius = 12;
    self.contentViewBorderColor = [UIColor ned_border];
    
    [self defaultStyle];
}

- (void)defaultStyle{
    self.spaceTitleToTop = 10;
    self.spaceTitleToContent = 5;
    self.spaceContentViewToButtons = 5;
    self.contentScrollHeightMin = 50;
    self.spaceContentMargin = 10;
    self.spaceTitleMargin = 10;
    self.spaceButtonsToBottom = 0;
    self.spaceBottomButtonMargin = 0;
    self.spaceBottomButtonVertical = 0;
    self.spaceBottomButtonHorizontal = 0;
    self.spaceContentTextToCustomView = 0;
    self.fontHeadTitle = [UIFont ned_pfmOfSize:18];
    self.showButtonsSeparationLine = YES;
}

/// 圆角按钮
- (void)cornerBorderButtonStyle{
    self.spaceTitleToTop = 15;
    self.spaceTitleToContent = 15;
    self.spaceContentViewToButtons = 15;
    self.contentScrollHeightMin = 50;
    self.spaceContentMargin = 10;
    self.spaceTitleMargin = 10;
    self.spaceButtonsToBottom = 15;
    self.spaceBottomButtonMargin = 10;
    self.spaceBottomButtonVertical = 15;
    self.spaceBottomButtonHorizontal = 15;
    self.spaceContentTextToCustomView = 0;
    self.fontHeadTitle = [UIFont ned_pfmOfSize:18];
    self.showButtonsSeparationLine = NO;
    self.showBottomButtonCorner = YES;
    self.bottomButtonBorderWidth = 1;
    self.buttonHeight = 40;
}

@end
